//
//  HCHomeBrandPreferenceView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/6/19.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCHomeBrandPreferenceView.h"
#import "HCHomeGrandPreferenceCell.h"

@interface HCHomeBrandPreferenceView()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    UIImageView *projectLogo;
    UIImageView *advertiseImageView;
}

@property (nonatomic, strong) UICollectionView *goodsCollectView;

@end

@implementation HCHomeBrandPreferenceView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initializeView];
    }
    return self;
}

- (void)initializeView {
    
    UIImage *im = [UIImage imageNamed:@"项目展示-1"];
    projectLogo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, YBKAdaptiveW(im.size.width), YBKAdaptiveH(im.size.height))];
    projectLogo.center = CGPointMake(Screen_width/2.0, projectLogo.center.y);
    projectLogo.image = im;
    projectLogo.backgroundColor = [UIColor clearColor];
    [self addSubview:projectLogo];

    advertiseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(projectLogo.frame), Screen_width, YBKAdaptiveH(149))];
    advertiseImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(advertiseClick)];
    [advertiseImageView addGestureRecognizer:tap];
    [self addSubview:advertiseImageView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(Screen_width/2.0, YBKAdaptiveH(125)/2.0);
    
    self.goodsCollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(advertiseImageView.frame), Screen_width, YBKAdaptiveH(125)) collectionViewLayout:layout];
    self.goodsCollectView.backgroundColor = [UIColor whiteColor];
    [self.goodsCollectView registerNib:[UINib nibWithNibName:@"HCHomeGrandPreferenceCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"grandCell"];
    self.goodsCollectView.delegate = self;
    self.goodsCollectView.dataSource = self;
    [self addSubview:self.goodsCollectView];
    
    UIView *horizeLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.goodsCollectView.frame)-YBKAdaptiveH(125)/2.0, Screen_width, 0.5)];
    horizeLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:horizeLine];
    
    UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(Screen_width/2, CGRectGetMinY(self.goodsCollectView.frame), 0.5, YBKAdaptiveH(125))];
    verticalLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:verticalLine];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 4;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"grandCell";
    HCHomeGrandPreferenceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        
        cell = [[HCHomeGrandPreferenceCell alloc] initWithFrame:CGRectZero];
    }
    
    cell.goodsIntegral.text = [NSString stringWithFormat:@"积分%@",[self.goodsArray[indexPath.item] objectForKey:@"price"]];
    [cell.goodsLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.goodsArray[indexPath.item] objectForKey:@"img"]]] placeholderImage:nil];
    cell.goodsName.text = [NSString stringWithFormat:@"%@",[self.goodsArray[indexPath.item] objectForKey:@"name"]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.didClickGoodsBlock) {
        
        self.didClickGoodsBlock(indexPath.item);
    }
}

- (void)advertiseClick {

    if (self.didClickAdvertiseBlock) {
        
        self.didClickAdvertiseBlock();
    }
}

- (void)setGoodsArray:(NSArray *)goodsArray {

    _goodsArray = goodsArray;
    if (goodsArray.count > 0) {
        
        [self.goodsCollectView reloadData];
    }
}

- (void)setAdvertiseImageUrl:(NSString *)advertiseImageUrl {

    _advertiseImageUrl = advertiseImageUrl;
    if (advertiseImageUrl.length != 0) {
        
        [advertiseImageView sd_setImageWithURL:[NSURL URLWithString:advertiseImageUrl] placeholderImage:nil];
    }
}

@end
